<?php

namespace App\Http\Controllers\Order;

use App\Model\Order\UserOrder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UserOrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            $input = $request->all();
            if (Auth::user()->is_superuser) {
                if (empty($input['start_time']) && empty($input['end_time'])) {
                    $orders = UserOrder::with(['MealSupplementBalances' => function ($query) {
                        $query->with('users:id,first_name,username');
                    }])->whereHas('MealSupplementBalances', function ($query) use ($input) {
                        $query->whereHas('users', function ($query) use ($input) {
                            $query->where('username', 'like', '%' . $input['username'] . '%')
                                ->where(function ($query) use ($input) {
                                    if (empty($input['first_name'])) {
                                        $query->whereNull('first_name')->orWhere('first_name', 'like', '%' . $input['first_name'] . '%');
                                    } else {
                                        $query->where('first_name', 'like', '%' . $input['first_name'] . '%');
                                    }
                                });
                        });
                    })->orderBY('created_at', 'desc')->get();
                } else {
                    $orders = UserOrder::with(['MealSupplementBalances' => function ($query) {
                        $query->with('users:id,first_name,username');
                    }])->whereHas('MealSupplementBalances', function ($query) use ($input) {
                        $query->whereHas('users', function ($query) use ($input) {
                            $query->where('username', 'like', '%' . $input['username'] . '%')
                                ->where(function ($query) use ($input) {
                                    if (empty($input['first_name'])) {
                                        $query->whereNull('first_name')->orWhere('first_name', 'like', '%' . $input['first_name'] . '%');
                                    } else {
                                        $query->where('first_name', 'like', '%' . $input['first_name'] . '%');
                                    }

                                });
                        });
                    })
                        ->whereDate('created_at', '>=', $input['start_time'])
                        ->whereDate('created_at', '<=', $input['end_time'])
                        ->orderBY('created_at', 'desc')->get();
                }
            } else {
                $orders = UserOrder::with(['MealSupplementBalances' => function ($query) {
                    $query->with('users:id,first_name,username');
                }])->whereHas('MealSupplementBalances', function ($query) use ($input) {
                    $query->whereHas('users', function ($query) use ($input) {
                        $query->where('username', 'like', '%' . Auth::user()->username . '%');
                    });
                })->orderBY('created_at', 'desc')->get();
            }

            return $this->success(200, $orders);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            return $this->success(200, ['index']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try {
            return $this->success(200, ['index']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            return $this->success(200, ['index']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            return $this->success(200, ['index']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }
}
